package com.iooiee.example_helloworld;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;

public class MinaServer {

    static int PORT = 8070;
    static IoAcceptor accept = null;
    public static void main(String[] args) throws IOException {
        accept = new NioSocketAcceptor();
        //IO模型中的过滤器主要处理编码解码,设置编码过滤器
        accept.getFilterChain().addLast("codec",new ProtocolCodecFilter(
                new TextLineCodecFactory(Charset.forName("UTF-8"), LineDelimiter.WINDOWS.getValue(),LineDelimiter.WINDOWS.getValue())
        ));
        accept.getSessionConfig().setReadBufferSize(1024);
        accept.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE,10);
        accept.setHandler(new MyServerHandler());
        accept.bind(new InetSocketAddress(PORT));
        System.out.println("Server >>>> 启动端口："+ PORT);
    }
}
